package com.cqdev.washer.api.controller;

import com.cqdev.washer.api.model.SyncTableCommand;
import com.cqdev.washer.biz.model.DbConfigBO;
import com.cqdev.washer.biz.model.TableBO;
import com.cqdev.washer.biz.service.DbConfigService;
import com.cqdev.washer.biz.service.TransferDataService;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @author yunfei.li
 *         类描述: TODO
 *         日期： 2018/7/9
 */
@Controller
@RequestMapping("/transfer")
public class TransferController {


    private final TransferDataService transferDataService;
    private final DbConfigService dbConfigService;

    public TransferController(
            @Qualifier("transferOverwriteDataServiceImpl") TransferDataService transferDataService, DbConfigService dbConfigService) {
        this.transferDataService = transferDataService;
        this.dbConfigService = dbConfigService;
    }

    @GetMapping("/database/list")
    public String showDataBases() {
        return null;
    }

    @GetMapping("/table/list")
    public String showTables() {
        return null;
    }

    @GetMapping("/sync")
    @ResponseBody
    public Object sync(SyncTableCommand syncTableCommand) {
        DbConfigBO sourceDB = dbConfigService.getOne(syncTableCommand.getSourceDBConfigId());
        DbConfigBO targetDB = dbConfigService.getOne(syncTableCommand.getTargetDBConfigId());
        TableBO sourceTable = new TableBO(sourceDB, syncTableCommand.getSourceTableName());
        TableBO targetTable = new TableBO(targetDB, syncTableCommand.getTargetTableName());
        int recordNum = transferDataService.syncTable(sourceTable, targetTable);
        return recordNum;
    }
}
